﻿using System;
using System.Collections.Generic;
using System.Text;

namespace myServer
{
    //
    // Command Format:
    //
    //     COMMAND = 
    //          ::<CMD TYPE>::<CMD NAME>[::PARAMETER1[::PARAMETER2]...]
    //      
    //
    

    public class CmdParser
    {
        // command type
        public enum CmdType
        { 
            INVALID = 0,
            SHELL,
            CONTROL,
            TEST,
        }

        // command name
        public enum CmdName
        {
            INVALID = 0,

            // shell
            LISTDRV,
            CHDRV,
            CHDIR,
            LISTDIR,
            LISTFILE,
            INFODRV,
            INFODIR,
            INFOFILE,
            DOWNLOAD,   // server -> client
            UPLOAD,     // client - > server

            // control
            RELOGIN,

            // test
            HANDSHAKE,
        }

        //
        // Reports the index of the number count of occurrence 
        // in string of the substring 
        //
        private static int IndexOfCount(string str, string substr, int count)
        {
            int pos = 0;
            for (int i = 1; i < count; i++)
                if (pos >= 0)
                    pos = str.IndexOf(substr, pos + 1);
                else
                    return pos;
            return pos;
        }

        public static CmdType GetCmdType(string cmdline)
        {
            if (!cmdline.Trim().Equals("") && cmdline.Trim().StartsWith("::"))
            {
                try
                {
                    string str = cmdline.Substring(cmdline.IndexOf("::") + 2);

                    int len = (str.IndexOf("::") > 0) ? str.IndexOf("::") : str.Length;

                    str = str.Substring(0, len);

                    if (!str.Trim().Equals(""))
                        return (CmdType)Enum.Parse(typeof(CmdType), str.Trim(), true);
                }
                catch (Exception)
                {
                    return CmdType.INVALID;
                }
            }

            return CmdType.INVALID;
        }

        public static CmdName GetCmdName(string cmdline)
        {
            if (!cmdline.Trim().Equals("") && cmdline.Trim().StartsWith("::"))
            {
                try
                {
                    string str = cmdline.Substring(cmdline.IndexOf("::") + 2);

                    int len = (str.IndexOf("::") > 0) ? str.IndexOf("::") + 2 : str.Length;

                    str = str.Substring(len);

                    len = (str.IndexOf("::") > 0) ? str.IndexOf("::") : str.Length;

                    str = str.Substring(0, len);

                    if (!str.Trim().Equals(""))
                        return (CmdName)Enum.Parse(typeof(CmdName), str.Trim(), true);
                }
                catch (Exception)
                {
                    return CmdName.INVALID;
                }
            }

            return CmdName.INVALID;
        }

        public static string GetPara1(string cmdline)
        {
            /*
            if (!cmdline.Trim().Equals("") && cmdline.Trim().StartsWith("::"))
            {
                try
                {
                    string str = cmdline.Substring(cmdline.IndexOf("::") + 2);

                    int len = (str.IndexOf("::") > 0) ? str.IndexOf("::") + 2 : str.Length;

                    str = str.Substring(len);

                    len = (str.IndexOf("::") > 0) ? str.IndexOf("::") + 2 : str.Length;

                    str = str.Substring(len);

                    len = (str.IndexOf("::") > 0) ? str.IndexOf("::") : str.Length;

                    str = str.Substring(0, len);

                    return str;
                }
                catch (Exception)
                {
                    return "";
                }
            }

            return "";
             * */

            return GetParaN(cmdline, 1);
        }

        public static string GetParaN(string cmdline, int n)
        {
            if (!cmdline.Trim().Equals("") && cmdline.Trim().StartsWith("::"))
            {
                try
                {
                    int pos = IndexOfCount(cmdline, "::", n + 2);

                    if (pos < 0)
                        return "";

                    int len = cmdline.IndexOf("::", pos + 1);

                    len = len > 0 ? len - pos - 2 : cmdline.Length - pos - 2;

                    return cmdline.Substring(pos + 2, len);
                }
                catch (Exception)
                {
                    return "";
                }
            }

            return "";
        }

    }
}
